<?php
/*
Description: This file is used to change login paassword.
*/
#########IncludeFiles####################################################################################
include_once '../inc/config.php';
include_once("../inc/bootstrape.php");
require_once _SITE_PATH_.'classes/Vendor/Vendor.php';
require_once _SITE_PATH_.'classes/Wedding/Wedding.php';
require_once _SITE_PATH_.'classes/VendorsMaster/VendorsMaster.php';
require_once _SITE_PATH_.'classes/CommonUtility/CommonUtility.php';
require_once _SITE_PATH_.'classes/Master/Master.php';

$errMsg = $_GET['err'];
$action = trim($_REQUEST['action']);
$id = trim($_REQUEST['id']); 
if(empty($id)){
    header("Location: "._SITE_ADMIN_URL_."wedding/wedding_mng.php");
    exit;
}

//$result = CommonUtility::getDataById($id, 'wedding_package');          
$res = Wedding::getWeddingPackagesById($id);
$result = $res[0];

$editor = CommonUtility::includeEditor();

if(isset($_POST['submit'])){
    // upload and create thumb of the image //
    if( !empty($_FILES['wedpg_days_image'] ) ){
        $arrTempHolder = array();
        foreach($_FILES['wedpg_days_image']['name'] as $key => $val){
            array_push($arrTempHolder,$val);
        }

        $arrWeddingScheduleIds = @explode(',',$_POST['hdnWeddingScheduleIds']);
        $filename = 'wedpg_days_image';
        $wedpg_days_image = CommonUtility::uploadNcreateThumb($filename, $module='wedpkg_days_specification', true, 100);
        
        $arrFinalUploadedImage = array();
        if(!empty($arrTempHolder)){
            $i = 0;
            $j = 0;
            foreach($arrTempHolder as $key => $val){
                if(empty($val)){
                    array_push($arrFinalUploadedImage,'');
                }else{
                    array_push($arrFinalUploadedImage,$wedpg_days_image[$j]);
                    unset($wedpg_days_image[$j]);
                    $j++;
                }
                $i++;
            }
        }
        
        if(!empty($arrFinalUploadedImage)){
            for($i=0; $i<count($arrFinalUploadedImage); $i++){
                if(empty($arrFinalUploadedImage[$i])){
                    continue;
                }
                $condition_wedd_schedule = array(); 
                $condition_wedd_schedule['id'] = $arrWeddingScheduleIds[$i];

                $params_wedd_scedule = array();
                $params_wedd_scedule['image'] = $arrFinalUploadedImage[$i];
                CommonUtility::updateData($params_wedd_scedule, 'wedding_schedule_specification', $condition_wedd_schedule);
            }
        }
    }
	
    //$parms['vendor_id'] = $_POST['vendor'];
    $parms['fixed_cost_items'] = @implode(',',$_POST['fixed_cost_items']);
    $parms['variable_cost_items'] = @implode(',',$_POST['variable_cost_items']);

    $parms['title'] = $_POST['title'];
    $parms['theme_id'] = $_POST['theme_id'];

    $parms['package_url'] = str_replace(' ','_',$_POST['title']);

    $parms['min_capacity'] = $_POST['min_capacity'];
    $parms['max_capacity'] = $_POST['max_capacity'];
    $parms['max_ceromonies'] = $_POST['max_ceromonies'];
    $parms['tags'] = $_POST['tags'];
    $parms['priority'] = $_POST['priority'];
    
    $parms['price'] = $_POST['price'];
    $parms['no_of_people'] = $_POST['no_of_people'];
    $parms['per_person_cost'] = $_POST['per_person_cost'];
    
    $parms['discount'] = $_POST['discount'];
    $parms['discount_type'] = $_POST['discount_type'];
    $parms['from_date'] = $_POST['from_date'];
    $parms['to_date'] = $_POST['to_date'];
    
    $parms['short_description'] = $_POST['short_description'];
    $parms['description'] = $_POST['description'];        
    $parms['booking_amount'] = $_POST['booking_amount'];

    $parms['cancellation_policy'] = $_POST['cancellation_policy'];
    $parms['tc_policy'] = $_POST['tc_policy'];
    
    $condition = array(); 
    $condition['id'] = $id;


    // upload and create thumb of the image //
    if( !empty( $_FILES['package_image'] ) ){
        $filename  = 'package_image';     //  this is name of the control you are using in html form         
        $module='wedpkg';

        $uploadedfile = CommonUtility::uploadNcreateThumb($filename, $module, true, 100, true);
        
        if( $uploadedfile!= -1 )    {
            $parms['package_image'] = $uploadedfile;//$_POST['package_image'];
        }        
    }
    
    // upload and create thumb of the image //
    if( !empty($_FILES['wedding_images'] ) ){
        $parms_wedding_images = array();
        $filename  = 'wedding_images';     //  this is name of the control you are using in html form    
        $uploadedfile = CommonUtility::uploadNcreateThumb($filename, $module='wedpkg', true, 100, true);
    
        if( $uploadedfile != -1 )    {
            $parms_wedding_images['entity_type'] = '3';
            $parms_wedding_images['entity_id'] = $id;
            
            //CommonUtility::deleteData('site_galleries', $parms_wedding_images);

            $i = 0;
            foreach($uploadedfile as $key => $val){
                $parms_wedding_images['image'] = $val;
                $parms_wedding_images['image_title'] = $_POST['image_title'][$i];
                $parms_wedding_images['image_tooltip'] = $_POST['image_tooltip'][$i];
                $parms_wedding_images['image_description'] = $_POST['image_description'][$i];

                $parms_wedding_images['added'] = date("Y-m-d");
                CommonUtility::insertData($parms_wedding_images,'site_galleries');
                $i++;
            }
            
        }
    }

    if( CommonUtility::updateData($parms, 'wedding_package', $condition)) {        
        header("Location: wedding_mng.php?msg=1036");            
        exit;
    }
    $errMsg = 1037;    
}

$fixed_cost_items = CommonUtility::getVendorServicesByVendorId($result['vendor_id'], @explode(',',$result['fixed_cost_items']));
$variable_cost_items = CommonUtility::getAllVendorServices(null, @explode(',',$result['variable_cost_items']));

/*$services_name = '';
if(!empty($result['vendor_services'])){
    $vendor_services =VendorsMaster::getVendorService($result['vendor_services']);
    $vendor_services_name = array();
    foreach($vendor_services as $key => $val){
        array_push($vendor_services_name, $val['name']);
    }
    $services_name = implode(',',$vendor_services_name);
}*/

$wedding_package_images = CommonUtility::getCommonImages($result['id'],'3');
$wedding_schedule = Wedding::getWeddingScheduleSpecificationByWeddingId($result['id']);

$wedding_schedule_ids = '';
if(!empty($wedding_schedule)){
    foreach($wedding_schedule as $key => $val){
        $wedding_schedule_ids.= $val['id'].',';
    }
    $wedding_schedule_ids = rtrim($wedding_schedule_ids,',');
}

if( $errMsg ){
    $result = $_POST;    
}else{
    $result['inclusion'] =  @explode(',',$result['inclusion']);
    $result['exclusion'] =  @explode(',',$result['exclusion']);
}
$wedding_inclusion = Master::getInclusion($result['inclusion']);

$wedding_exclusion = Master::getInclusion($result['exclusion']);

$_tplName = 'wedding/wedding_edit.tpl';
$_title = 'Edit Wedding Package';

//  calender js include
$_extraJS[] = _SITE_URL_ . 'lib/calender/js/cal.js';
require_once _ADMIN_TPL_MAIN_;
?>